package com.fanshuai2.niuke.strings;

import java.util.Scanner;

/**
 * 查找两个字符串a,b中的最长公共子串。若有多个，输出在较短串中最先出现的那个。
 * 注：子串的定义：将一个字符串删去前缀和后缀（也可以不删）形成的字符串。请和“子序列”的概念分开！
 *
 * 输入描述：
 * 输入两个字符串
 *
 * 输出描述：
 * 返回重复出现的字符
 */
public class LongestCommonString {
    static String longestCommonS(String a, String b) {
        //替换为短字符串在前面
        if (a.length() > b.length()) {
            String t = a;
            a = b;
            b = t;
        }

        int lena = a.length();
        int lenb = b.length();
        char[] chsa = a.toCharArray();
        char[] chsb = b.toCharArray();

        int commonLong = 0;
        String common = "";
        for (int i = 0; i < lena; i++) {
            int i0 = i;
            for (int j = 0; j < lenb; j++) {
                int j0 = j;
                while (i0 < lena && j0 < lenb && chsa[i0] == chsb[j0]) {
                    i0++;
                    j0++;
                }
                if (i0 -i > 0) {
                    if (i0 - i > commonLong) {
                        commonLong = i0 - i;
                        common = a.substring(i, i0);
                    }
                }
                i0 = i;
            }
        }
        return common;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextLine()) {
            String a = in.nextLine();
            String b = in.nextLine();
            System.out.println(longestCommonS(a, b));
        }
    }
}
